package com.bh.kysys.service.impl;

import com.bh.common.core.domain.entity.SysUser;
import com.bh.common.core.domain.model.LoginUser;
import com.bh.common.utils.StringUtils;
import com.bh.kysys.domain.JcSysOrganization;
import com.bh.kysys.domain.JcSysUserOrganization;
import com.bh.kysys.mapper.JcSysOrganizationMapper;
import com.bh.kysys.mapper.JcSysUserOrganizationMapper;
import com.bh.kysys.service.IJcSysUserOrganizationService;
import com.bh.kysys.vo.JcSysOrganizationVo;
import com.bh.kysys.vo.JcSysUserOrganizationVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;

import static com.bh.common.utils.SecurityUtils.getLoginUser;

/**
 * organizationService业务层处理
 *
 * @author slx
 * @date 2023-03-11
 */
@Service
public class JcSysUserOrganizationServiceImpl implements IJcSysUserOrganizationService {
    @Autowired
    private JcSysUserOrganizationMapper jcSysUserOrganizationMapper;
    @Autowired
    private JcSysOrganizationMapper jcSysOrganizationMapper;

    /**
     * 查询organization
     *
     * @param userId organization主键
     * @return organization
     */
    @Override
    public List<JcSysUserOrganization> selectJcSysUserOrganizationByUserId(Long userId) {
        if (SysUser.isAdmin(userId)) {
            List<JcSysUserOrganization> userOrganizations = new ArrayList<>();
            //处理所有数据
            List<JcSysOrganization> organizations = jcSysOrganizationMapper.selectJcSysOrganizationList(new JcSysOrganization());
            for (JcSysOrganization organization : organizations) {
                JcSysUserOrganization jcSysUserOrganization = new JcSysUserOrganization();
                jcSysUserOrganization.setJcSysOrganizationId(organization.getId());
                jcSysUserOrganization.setUserId(userId);
                userOrganizations.add(jcSysUserOrganization);
            }
            return userOrganizations;
        }
        return jcSysUserOrganizationMapper.selectJcSysUserOrganizationListByUserId(userId);
    }

    @Override
    public List<JcSysUserOrganization> selectJcSysUserOrganizationByOrgId(Long orgId) {
        return jcSysUserOrganizationMapper.selectJcSysUserOrganizationByOrgId(orgId);
    }

    /**
     * 查询organization列表
     *
     * @param jcSysUserOrganization organization
     * @return organization
     */
    @Override
    public List<JcSysUserOrganization> selectJcSysUserOrganizationList(JcSysUserOrganization jcSysUserOrganization) {
        return jcSysUserOrganizationMapper.selectJcSysUserOrganizationList(jcSysUserOrganization);
    }

    @Override
    public int saveBatch(List<JcSysUserOrganization> userOrganizations) {
        return jcSysUserOrganizationMapper.saveBatch(userOrganizations);
    }

    @Override
    public int insert(JcSysUserOrganization userOrganization) {
        List<JcSysUserOrganization> userOrganizations = new ArrayList<>();
        userOrganizations.add(userOrganization);
        return jcSysUserOrganizationMapper.saveBatch(userOrganizations);
    }

    @Override
    public int batchAddOrg(JcSysUserOrganizationVo organization) {
        // 先删除
        if (!SysUser.isAdmin(organization.getUserId())) {
            Long[] orgIds = organization.getOrganizationIds();
            List<JcSysUserOrganization> organizations = new ArrayList<>();
            for (Long orgId : orgIds) {
                JcSysUserOrganization sysUserOrganization = jcSysUserOrganizationMapper.selectJcSysUserOrganizationByUserIdAndOrgId(organization.getUserId(), orgId);
                if (sysUserOrganization == null) {
                    JcSysUserOrganization newOrg = new JcSysUserOrganization();
                    newOrg.setUserId(organization.getUserId());
                    newOrg.setJcSysOrganizationId(orgId);
                    organizations.add(newOrg);
                }
            }
            //新增
            if (!organizations.isEmpty()) {
                return jcSysUserOrganizationMapper.saveBatch(organizations);
            }
        }
        return 0;
    }

    @Override
    public int deleteUserOrg(JcSysUserOrganization organization) {
        //先删除
        return jcSysUserOrganizationMapper.deleteJcSysUserOrganization(organization);
    }
}
